package string_question.leecode.test10;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/6 18:46
 */
public class Solution1 {

    // 动态规划求解两个字符串是否匹配问题
    public boolean isPick(String s, String p) {
        if (("").equals(p)) return ("").equals(s);
        int lenS = s.length();
        int lenP = p.length();
        boolean[][] dp = new boolean[lenS + 1][lenP + 1];
        dp[0][0] = true;
        for (int i = 2; i <= lenP; i++) {
            if (p.charAt(i - 1) == '*') {
                dp[0][i] = dp[0][i - 2];
            }
        }
        for (int i = 1; i <= lenS; i++) {
            for (int j = 1; j <= lenP; j++) {
                if (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.') {
                    dp[i][j] = dp[i - 1][j - 1];
                } else if (p.charAt(j - 1) == '*') {
                    if (j == 1) {
                        continue;
                    } else if (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.') {
                        dp[i][j] = dp[i][j - 2] || dp[i][j - 1] || dp[i - 1][j];   // 注意这里的j-2
                    } else {
                        dp[i][j] = dp[i][j - 2];
                    }
                }
            }
        }
        return dp[lenS][lenP];
    }
}
